#parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

# make subject binomial key

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/subject_monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture $permutation_str \
    --make-start-point -start-point $output_dir/subject_start_point_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -start-point $output_dir/subject_start_point_file.xf -monomial-key $output_dir/subject_monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-binomial-key -binomial-key $output_dir/subject_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# make monomial and binomial key for certificate authority

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture $permutation_str \
    --make-start-point -start-point $output_dir/start_point_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -start-point $output_dir/start_point_file.xf -monomial-key $output_dir/monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-binomial-key -binomial-key $output_dir/binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# make asymmetric_certificate

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/monomial_key_file.xf -binomial-key $output_dir/binomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-certificate-asymmetric -subject-binomial-key $output_dir/subject_binomial_key_file.xf -subject-str \"Alice binomial key\" -asymmetric-certificate $output_dir/asymmetric_certificate_file.xf  -start-timestamp $(date +%s) -end-timestamp $(expr $(date +%s) + 100)
"
echo ""; echo $str; echo ""
eval $str

# check certificate

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -binomial-key $output_dir/binomial_key_file.xf -asy-certificate $output_dir/asymmetric_certificate_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/lib_info_log_file.xf \
    --check-certificate-asymmetric
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_log_file.xf ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_log_file.xf\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# generate a wrong binomial key

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/wrong_monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -start-point $output_dir/start_point_file.xf -monomial-key $output_dir/wrong_monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --make-binomial-key -binomial-key $output_dir/wrong_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

# check certificate

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -binomial-key $output_dir/wrong_binomial_key_file.xf -asy-certificate $output_dir/asymmetric_certificate_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/wrong_lib_info_log_file.xf \
    --check-certificate-asymmetric
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/wrong_lib_info_log_file.xf ]; then echo FAIL ; exit ; fi ; \
if grep -q PASS \"$output_dir/wrong_lib_info_log_file.xf\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# check certificate content

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -asy-certificate $output_dir/asymmetric_certificate_file.xf \
    --permutation-conf -arch $architecture \
    --convert-asymmetric-certificate-to-binomial-key -binomial-key $output_dir/certificate_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/original_binomial_key.json \
    --print-header -xrn-file $output_dir/subject_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture \
    --logging-conf -lib-info-log $output_dir/certificate_binomial_key_file.json \
    --print-header -xrn-file $output_dir/certificate_binomial_key_file.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/certificate_binomial_key_file.json $output_dir/original_binomial_key.json || echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

